#ifndef TMDB_H
#define TMDB_H

#include <QObject>
#include <QString>
#include <QUrl>
#include <QVector>

#include <QNetworkAccessManager>
#include <QNetworkReply>

#include "defines.h"
#include "movie.h"

class Tmdb : public QObject
{
    Q_OBJECT
public:
    struct LookupResult
    {
        int tmdb_id;
        QString title;
        QByteArray cover;
        QString year;
    };

    typedef QVector<LookupResult> results;

    enum error { none = 0, fatal = 1, warning = 2 };

    ///
    /// Constructor
    explicit Tmdb(QObject *parent = 0);

    ///
    /// @return     The error as string
    ///
    /// Returns the error string for human reading
    inline const QString errorString() const { return errorString_; }

    ///
    /// @param  search      The movie which be searched on TMDB
    ///
    /// Movie lookup for TMDB
    void lookupMovie(Movie *movie);

    results result() const { return results_; }

private slots:
    ///
    /// @param  reply       A pointer the the network response
    ///
    /// Handles the response for each request
    void requestFinished(QNetworkReply *reply);

private:
    ///
    /// Load the configuration from TMDB. Its a blocked call.
    void loadConfiguration();

    ///
    /// @param  path    The backdrop path for an image at TMDB
    ///
    /// Loads a Image from TMDB
    const QByteArray loadImage(const QString &path);

    ///
    /// @param  reply       The response from configuration request
    ///
    /// Parse the TMDB configuration request
    void parseConfigurationRequest(QNetworkReply *reply);

    ///
    /// @return     The full Url to load the configuration
    ///
    /// Returns the Url with API key for the request to load
    /// the configuration for TMDB
    const QUrl getConfigurationUrl();

    ///
    /// @param  path    The backdrop path for an image at TMDB
    ///
    /// @return         The full Url to the image
    ///
    /// Returns the full api supported irmage url
    const QUrl getImageUrl(const QString &path);

    ///
    /// @param  search      The movie search string
    ///
    /// @return     The full Url to search a movie
    ///
    /// Returns the Url with API key for the request to load
    /// specified movie data, if the movie could be find
    const QUrl getMovieSearchUrl(const QString &search);


    QNetworkAccessManager *manager_;            ///< The manager for the network access
    QString apiKey_;                            ///< Holds the API key for TMDB
    QString imageBaseUrl_;                      ///< Holds the base url for image
    int errorCode_;                             ///< Holds the current error code (0 = none)
    QString errorString_;                       ///< Holds the error string for human reading
    bool configOk_;                             ///< True if the config from TMDB ok
    QMap<QString, int> requestData_;            ///< Holds the asynchronouse request data
    Movie *movie_;                              ///< Holds the movie for lookup
    results results_;                             ///< Vector of TMDB resultss

    static const QString API_MASTER_DOMAIN;     ///< The TMDB API master domain
    static const QString API_VERSION;           ///< The TMDB API version suffix
    static const QString API_CONFIGURATION;     ///< The TMDB API configuration suffix
    static const QString API_SEARCH_MOVIE;      ///< The TMDB API search movie suffix
};

#endif // TMDB_H
